/*
 * @(#)ProductServiceImpl.java 2021年3月8日
 *
 * Copyright (c) 2010 by rayootech.com. All rights reserved.
 */
package com.nuctech.product.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.nuctech.common.entity.Product;
import com.nuctech.product.dao.ProductDao;
import com.nuctech.product.service.ProductService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商品业务接口实现类
 *
 * @author lilu
 * @date 2021年3月8日
 * @since 1.0.0
 */
@Service
public class ProductServiceImpl implements ProductService {

	@Autowired
	private ProductDao productDao;

	@Override
	public Product findByPid(Integer pid) {
		return this.productDao.findById(pid).get();
	}

	@Override
	@Transactional(rollbackFor = {Exception.class, RuntimeException.class})
	public Integer reduceInventery(Integer pid, Integer number) {
 		Product product = this.findByPid(pid);

		int i = product.getStock() - number;
		if (pid % 2 == 1) {
			i = 1 / 0;
		}
		product.setStock(i);

		this.productDao.save(product);

		return i;
	}

}
